package com.yj.exception;

import com.yj.common.responses.TerminalResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @Author: yesiming
 * @Date: 下午11:31 2017/12/21
 */
@RestController
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler({Exception.class})
    @ResponseStatus(HttpStatus.OK)
    public TerminalResponse<Object> processException(Exception exception) {
        logger.info("------Exception------");

        return new TerminalResponse<>(false, "Exception", exception.getMessage());
    }

    @ExceptionHandler({RuntimeException.class})
    @ResponseStatus(HttpStatus.OK)
    public TerminalResponse<Object> processException(RuntimeException exception) {
        logger.info("------RuntimeException------");

        return new TerminalResponse<>(false, "RuntimeException", exception.getMessage());
    }

    @ExceptionHandler(ServletRequestBindingException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public TerminalResponse<Object> handleServletRequestBindingException(ServletRequestBindingException e) {
        logger.info("------ServletRequestBindingException------");

        return new TerminalResponse<>(false, "ServletRequestBindingException", e.getMessage());
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public TerminalResponse<Object> handleMissingParameterError(MissingServletRequestParameterException e) {
        logger.info("------MissingServletRequestParameterException------");

        Map<String, String> error = new HashMap<>();
        error.put("错误信息", e.getMessage());
        error.put("缺失参数名称", e.getParameterName());
        error.put("缺失参数类型", e.getParameterType());

        return new TerminalResponse<>(false, "MissingServletRequestParameterException", error);
    }

    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public TerminalResponse<Object> handleBindException(BindException e) {
        logger.info("------BindException------");

        List<Map<String, Object>> errorList = new ArrayList<>();

        for (FieldError fieldError : e.getFieldErrors()) {
            HashMap<String, Object> errorMap = new HashMap<>();
            errorMap.put("报错字段", fieldError.getField());
            errorMap.put("错误原因", fieldError.getDefaultMessage());

            errorList.add(errorMap);
        }

        return new TerminalResponse<>(false, "BindException", errorList);
    }
}
